package com.david.concurrent.notify.wait;

/**
 * @author wade
 * @version 1.0
 * @date 2021/7/19 16:48
 */
public class DeadLock {

    private static final Object resourceA =new Object ();
    private static final Object resourceB =new Object() ;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (resourceA){
                System.out.println( "t1 get resourceA lock ");
                synchronized (resourceB){
                    System.out.println("t1 get resourceB lock ");
                    //线程A阻塞，并择放获取到的 resourceA的锁
                    System.out.println ("t1 release resourceA lock" ) ;
                    try {
                        resourceA.wait() ;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (resourceA){
                System.out.println( "t2 get resourceA lock ");
                System.out.println( "t2 try get resourceB lock ");
                synchronized (resourceB){
                    System.out.println("t2 get resourceB lock ");
                    //线程A阻塞，并择放获取到的 resourceA的锁
                    System.out.println ("t2 release resourceA lock" ) ;
                    try {
                        resourceA.wait() ;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("main over!");
    }
}
